<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<jk-button icon="el-icon-printer" @click="onPrintBtn">打印</jk-button>
				</div>
				<div class="flex-end-center-wrap">
					<jk-select v-model="searchParams.workshopId" :option-list="workshopList" :clearable="false" placeholder="区域" class="search-bar-item-size2 margin-5-0 item-gap"></jk-select>
					<day-switch v-model="searchParams.belongDate" size="small" class="item-gap"></day-switch>
					<div class="search-bar-item-size2 item-gap">
						<el-date-picker v-model="searchParams.belongDate" :clearable="false" size="small" style="width: 100%;" type="date" value-format="yyyy-MM-dd" placeholder="日期" />
					</div>
					<!--<jk-select v-model="searchParams.shiftId" clearable placeholder="班次" :option-list="shiftList" size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>-->
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>
		<jk-card v-loading="tableLoading">
			<table-list
				:search-params="searchParams"
				:report-data.sync="reportData"
				:table-height="tableHeight"
			></table-list>
		</jk-card>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { formatDate } from '/src/utils/jk/jk-tool';
    import handlerCache from '../../../mixins/handler-cache';
    import { productionSpunYarnOutputGetByDate } from '../../../api/production/reports';
    import { workforcemanagementShiftPage } from '../../../api/schedule/shift';
    import tableList from './components/table-list';
    import daySwitch from '../../jk-components/jk-day-switch/index';
    export default {
        name: 'XsAndLtDailyReport',
        components: {
            daySwitch,
            tableList
        },
        mixins: [calHeight, handlerCache],
        data() {
            return {
                apiItem: {
                    'page': productionSpunYarnOutputGetByDate
                },
                dateList: [],
                shiftList: [],
                workshopList: [],
                tableLoading: true,
                reportData: {
                    remark: '',
                    packBatchData: '',
                    processOutputList: [],
                    scheduleShiftMachineList: [],
                    xsDayOutput: 0, // 细纱日开台产量
                    xsDayQty: 0,
                    zlDayQty: 0 // 络筒日开台产量
                },
                searchParams: {
                    belongDate: '',
                    workshopId: null
                },
                dateNum: 0
            };
        },
        mounted() {
            this.getDependentData();
        },
        methods: {
            onPrintBtn() {
                const routeUrl = this.$router.resolve({
                    name: 'printXsAndLtDailyReport',
                    query: this.searchParams
                });
                window.open(routeUrl.href, '_blank');
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            getShiftListRequest() {
                return workforcemanagementShiftPage({ auditState: 1 }).then(res => {
                    if (res.data.status === 200) {
                        this.shiftList = res.data.res || [];
                        // this.$set(this.searchParams, 'shiftId', this.shiftList && this.shiftList.length ? this.shiftList[0].id : null);
                    }
                });
            },
            async getDependentData() {
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                this.$set(this.searchParams, 'belongDate', this.$route.query.belongDate || formatDate(Date.now() - 24 * 60 * 60 * 1000));
                this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);

                this.tableLoading = true;
                await this.getShiftListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                const params = { ...this.searchParams };
                return this.apiItem['page'](params).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        const resData = res.data.res || {};
                        if (resData.postList) {
                            resData.postList.forEach(x => {
                                x.empNum = x.empList ? x.empList.length : 0;
                            });
                        }
                        this.reportData = resData;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.getListRequest();
            }
        }
    };
</script>
